package com.xc.utils.email;

import com.xc.utils.PropertiesUtil;

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

/**
 *  mail sender
 *      阿里云服务器禁用了25端口，所以改为465端口采用SSL协议传输邮件
 * @author mipha
 * @version 1.0
 * @date 2019/3/7 7:37 PM
 */
public class MailSender {

    private final Properties prop = new Properties();
    private final Session session;
    private final Message msg;
    private final Transport transport;

    //构建器
    public static class Builder{
        private final String mailContent;
        private final String toAddress;

        private String debug = "false";
        private String auth = "true";
        private String host = "smtp.163.com";
        private String protocol = "smtp";

        private String subject = PropertiesUtil.getProperty("admin.auth.email.subject");
        //发件人地址
        private String fromAddress= PropertiesUtil.getProperty("admin.auth.email");
        //发件账户密码
        private String fromCount = PropertiesUtil.getProperty("admin.auth.email");
        private String fromPassword = PropertiesUtil.getProperty("admin.auth.email.pwd");



        public Builder Debug(String debug) {
            this.debug = debug;
            return this;
        }
        public Builder Subject(String subject) {
            this.subject = subject;
            return this;
        }
        public Builder Auth(String auth) {
            this.auth = auth;
            return this;
        }
        public Builder Host(String host) {
            this.host = host;
            return this;
        }
        public Builder FromCount(String fromCount) {
            this.fromCount = fromCount;
            return this;
        }
        public Builder FromAddress(String fromAddress) {
            this.fromAddress = fromAddress;
            return this;
        }
        public Builder FromPassword(String fromPassword) {
            this.fromPassword = fromPassword;
            return this;
        }
        public Builder(String mailContent, String toAddress) {
            this.mailContent = mailContent;
            this.toAddress = toAddress;
        }
        public Builder Protocol(String protocol) {
            this.protocol = protocol;
            return this;
        }
        public MailSender send() throws Exception{
            return new MailSender(this);
        }

    }
    private MailSender(Builder builder) throws Exception{
        //  是否开启调试
        prop.setProperty("mail.debug", builder.debug);
        //  是否开启鉴权
        prop.setProperty("mail.smtp.auth", builder.auth);

        prop.setProperty("mail.host", builder.host);
        prop.setProperty("mail.transport.protocol",builder.protocol);

        //阿里云服务器禁用25端口，所以服务器上改为465端口
        prop.put("mail.smtp.socketFactory.port", "465");
        prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        prop.setProperty("mail.smtp.socketFactory.port", "465");
        //  如果本机可发送 服务器不行，如下
//        解决方法：在/etc/hosts中添加127.0.0.1本地映射
//        1.在linux终端输入  hostname
//        2.打开/etc/hosts编辑添加一行  127.0.0.1  iZwz9dgm0ak5eyqrkwypkjZ
//        3.再输入  hostname -i

        session = Session.getInstance(prop);
        msg = new MimeMessage(session);

        transport = session.getTransport();
        msg.setSubject(builder.subject);
        msg.setFrom(new InternetAddress(builder.fromAddress,"通知邮件"));
        transport.connect(builder.fromCount,builder.fromPassword);

        //在这里contentType要设置成text/html,编码格式也要视情况而设定
        msg.setContent(builder.mailContent, "text/html;charset=utf-8");

        transport.sendMessage(msg, new Address[] {new InternetAddress(builder.toAddress)});
    }



}


